package server

import (
	core "carmanager/db"
	model "carmanager/models"
	"carmanager/models/dto"
	"carmanager/models/vo"
	"strings"
	"time"
)

type RoleService struct{}

func (r *RoleService) GetList(role model.Role, parms dto.PageParms) vo.PageInfo {
	db := core.GetDB()
	start := (parms.PageIndex - 1) * parms.PageSize
	data := new([]model.Role)
	Total := new(int64)
	query := db
	if !strings.EqualFold(role.RoleName, "") {
		query = query.Where("roleName like ?", "%"+role.RoleName+"%")
	}
	query.Model(model.Role{}).Count(Total)
	query.Limit(parms.PageSize).Offset(start).Find(data)

	return vo.PageInfo{
		TotalNumber: *Total,
		Data:        data,
	}
}

func (r *RoleService) Update(model model.Role) bool {
	db := core.GetDB()
	model.UpdateTime = time.Now()
	return db.Updates(&model).Error != nil
}
func (r *RoleService) AddRole(model model.Role) bool {
	db := core.GetDB()
	model.CreateTime = time.Now()
	model.UpdateTime = time.Now()
	model.Roleid = 0
	return db.Create(&model).Error != nil
}
func (r *RoleService) Del(id int) bool {
	db := core.GetDB()
	return db.Model(&model.Role{}).Delete("id=?", id).Error != nil
}
